﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// Qt headers
#include <QPainter>
#include <QRect>
#include <QMargins>
#include <QColor>

// custom headers
#include "PaintingUtils.h"

#include "DebugMessage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    void PaintingUtils::DrawBorder(QPainter * painter, QRect rect, QMargins border, QMargins cornerRadius, QColor lineColor, QColor bgColor)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(painter, L"painter为空", L"GMEP", L"2024-01-30");
        painter->save();

        // draw border
        // border-left, corner-left
        if (border.left())
        {
            painter->setPen(QPen(lineColor, border.left()));
            painter->drawLine(
                rect.bottomLeft() - QPointF(0, cornerRadius.bottom()),
                rect.topLeft() + QPointF(0, cornerRadius.left())
            );
            if (cornerRadius.left())
            {
                int extent = 2 * cornerRadius.left();
                painter->drawArc(QRectF(rect.topLeft(), QSize(extent, extent)), 90 * 16, 90 * 16);
            }
        }
        // border-top, corner-top
        if (border.top())
        {
            painter->setPen(QPen(lineColor, border.top()));
            painter->drawLine(
                rect.topLeft() + QPointF(cornerRadius.left(), 0),
                rect.topRight() - QPointF(cornerRadius.top(), 0)
            );
            if (cornerRadius.top())
            {
                int extent = 2 * cornerRadius.top();
                painter->drawArc(QRectF(rect.topRight() - QPointF(extent, 0), QSize(extent, extent)), 0, 90 * 16);
            }
        }
        // border-right, corner-right
        if (border.right())
        {
            painter->setPen(QPen(lineColor, border.right()));
            painter->drawLine(
                rect.topRight() + QPointF(0, cornerRadius.top()),
                rect.bottomRight() - QPointF(0, cornerRadius.right())
            );
            if (cornerRadius.right())
            {
                int extent = 2 * cornerRadius.right();
                painter->drawArc(QRectF(rect.bottomRight() - QPointF(extent, extent), QSize(extent, extent)), 270 * 16, 90 * 16);
            }
        }
        // border-bottom, corner-bottom
        if (border.bottom())
        {
            painter->setPen(QPen(lineColor, border.bottom()));
            painter->drawLine(
                rect.bottomRight() - QPointF(cornerRadius.right(), 0),
                rect.bottomLeft() + QPointF(cornerRadius.bottom(), 0)
            );
            if (cornerRadius.bottom())
            {
                int extent = 2 * cornerRadius.bottom();
                painter->drawArc(QRectF(rect.bottomLeft() - QPointF(0, extent), QSize(extent, extent)), 180 * 16, 90 * 16);
            }
        }

        // draw background
        painter->fillRect(rect.adjusted(border.left(), border.top(), -border.right(), -border.bottom()), bgColor);

        painter->restore();
    }

    QRect PaintingUtils::GetTextRect(QRect containerRect, QString text, QFont font, int textAlignment)
    {
        QFontMetrics fontMetric(font);
        QRect localTextRect = fontMetric.tightBoundingRect(text);

        // measure xOffset
        int xOffset(0);
        if ((textAlignment & Qt::AlignmentFlag::AlignLeft) == Qt::AlignmentFlag::AlignLeft)
        {
            xOffset = 0;
        }
        else if ((textAlignment & Qt::AlignmentFlag::AlignRight) == Qt::AlignmentFlag::AlignRight)
        {
            xOffset = containerRect.width() - localTextRect.width();
        }
        else if ((textAlignment & Qt::AlignmentFlag::AlignHCenter) == Qt::AlignmentFlag::AlignHCenter)
        {
            xOffset = (containerRect.width() - localTextRect.width()) / 2;
        }

        // measure yOffset
        int yOffset(0);
        if ((textAlignment & Qt::AlignmentFlag::AlignTop) == Qt::AlignmentFlag::AlignTop)
        {
            yOffset = 0;
        }
        else if ((textAlignment & Qt::AlignmentFlag::AlignBottom) == Qt::AlignmentFlag::AlignBottom)
        {
            yOffset = containerRect.height() - localTextRect.height();
        }
        else if ((textAlignment & Qt::AlignmentFlag::AlignVCenter) == Qt::AlignmentFlag::AlignVCenter)
        {
            yOffset = (containerRect.height() - localTextRect.height()) / 2;
        }

        return QRect(containerRect.topLeft() + QPoint(xOffset, yOffset), localTextRect.size());
    }
}